package top.lvkun.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.basic.utils.PageList;
import top.lvkun.org.domain.Department;
import top.lvkun.org.query.DepartmentQuery;
import top.lvkun.org.service.IDepartmentService;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;

    @GetMapping("{id}")
    @ApiOperation(value = "通过部门id查询",notes = "通过部门id查询")
    public Department getById(@PathVariable("id") Long id){
        return departmentService.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null) {
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作繁忙！"+e.getMessage());
        }
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除数据",notes = "通过部门id删除数据")
    public AjaxResult delById(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作繁忙！"+e.getMessage());
        }
    }

    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "查询所有")
    public List<Department> getAll(){
        return departmentService.getAll();
    }

    //分页+高级查询
    @PostMapping
    @ApiOperation(value = "分页+高级查询",notes = "参数DepartmentQuery")
    public PageList<Department> getPage(@RequestBody DepartmentQuery query){
        return departmentService.queryData(query);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "参数ids")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作繁忙！"+e.getMessage());
        }
    }

    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树",notes = "")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }

}
